package com.boxin.ims.modules.momarketing.common;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.Random;
import java.util.UUID;

import com.boxin.framework.base.utils.Constants;
import com.boxin.framework.base.utils.DateUtils;
import com.boxin.framework.base.utils.QRCodeGenerator;
import com.boxin.ims.modules.momarketing.entity.QRCode;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.utils.FileUtils;

/**
 * @author Jakemanse
 * 二维码生成工具类
 */
public class QRCodeUtils {
	
	
	public   static final String QRCODE_JPG_URL_PRIX = "http://hsm.hbweiyinqing.cn/huisimeng/servlet/qrc?qrcodeId=";
	public   static final String QRCODE_PNG_URL_PRIX = "http://hsm.hbweiyinqing.cn/huisimeng/servlet/qrc?qrcodeId=";
	public   static final String QRCODE_PNG_PAGE_URL_PRIX = "http://hsm.hbweiyinqing.cn/huisimeng/f/wechat/lottery/viewqrcode?id=";
	private static final String  QRCODE_URL_GIFT_PRIX = "http://hsm.hbweiyinqing.cn/huisimeng/";
	
	public static final int WIDTH = 720;		//720
	public static final int HEIGHT = 400;		//400
	
	
	public static final String QRCODE_VTYPE_SECKILL = "SKL";					//秒杀
	public static final String QRCODE_VTYPE_LOTTERY = "LTR";					//抽奖,奖品
	public static final String QRCODE_VTYPE_EXCHANGE = "SEG";					//积分兑换
	public static final String QRCODE_VTYPE_TICKET = "TCK";						//门票
	public static final String QRCODE_VTYPE_COUPON = "CPN";						//优惠券
	public static final String QRCODE_VTYPE_CIRCLE_SHARE = "CCS";				//朋友圈子转发
	public static final String QRCODE_VTYPE_PRAISE_ACTIVITY = "PRA";			//赞活动
	public static final String QRCODE_VTYPE_MEMBER_CARD = "MBR";				//会员卡
	public static final String QRCODE_VTYPE_PAY_MONEY   = "PAY";			    //消息、支付二维码
	
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-5-28  下午2:08:18
	 * @function <p> 生成png 图片 </p>
	 * @param content
	 * @param path
	 * @param width
	 * @param height
	 * @return
	 */
	public static boolean generateQRCodePNG(String content,String path,int width,int height){
		return generateQRCode("png",content,path,width,height);
	}
	
	
	/**
	 * @author Jakemanse
	 * @time 2014-3-29  上午11:08:57
	 * @function <p> 无边框距离  </p>
	 * @param content
	 * @param path
	 * @param width
	 * @param height
	 * @return
	 */
	public static boolean generateNoMarginQRCodePNG(String content,String path,int width,int height){
		return generateNoMarginQRCode("png",content,path,width,height);
	}
	
	
	
	
	/**
	 * @author Jakemanse
	 * @time 2014-1-28  下午11:33:40
	 * @function <p> 根据内容生成默认大小的 二维码  </p>
	 * @param content
	 * @return
	 */
	public static String  generateDefaultQRCodePNG(String content){
		
		//生成二维码
		QRCode qrcode = new QRCode();
		qrcode.setContent(content);
		String path = Constants.getWechatResourceSavePath();
		path += DateUtils.getTodayString()+File.separator+content;
		FileUtils.createDirectory(path);
		QRCodeUtils.generateQRCodePNG(content, path, WIDTH,HEIGHT);
		
		return path;
	}
	
	public static boolean generateQRCodeJPEG(String content,String path,int width,int height){
		return generateQRCode("jpeg",content,path,width,height);
	}
	
	
	
	
	private  static boolean   generateQRCode(String picType,String content,String path,int width,int height){
		return QRCodeGenerator.generateQRCode(picType, content, path, width, height);
	}
	
	
	private  static boolean   generateNoMarginQRCode(String picType,String content,String path,int width,int height){
		return QRCodeGenerator.generateNoMarginQRCode(picType, content, path, width, height);
	}
	
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-8-6  上午2:06:34
	 * @function <p>  容错率生成二维码 </p>
	 * @param picType
	 * @param content
	 * @param path
	 * @param width
	 * @param height
	 * @param errorCode
	 * @return
	 */
	public  static boolean   generateQRCodeWithErrorLevel(String picType,String content,String path,int width,int height,String errorCode){
		
		return QRCodeGenerator.generateQRCodeWithErrorLevel(picType, content, path, width, height, errorCode);
	}
	
	
	
	
	public static File  downloadImg(String strurl,File file){
		try {
			//String site ="http://qr.liantu.com/api.php?text=http://weixin.zhaotang.net/zt/f/gift/15.html&bg=FFFFFF&fg=000000&el=h&w=200&logo=http://42.96.189.145/upload/58.jpg";
			URL url = new URL(strurl);
			
			InputStream is = url.openStream();
			//File file = new File("D:\\temp\\myqrcode.jpg");
			if(!file.exists()){
				file.createNewFile();
			}
			
			BufferedInputStream bis = new BufferedInputStream(is);
			FileOutputStream fos  = new FileOutputStream(file);
			
			byte bytes[] = new byte[1024];
			int offset = 0;
			while ((offset = bis.read(bytes)) != -1) {
				fos.write(bytes, 0, offset);
			}
			
			
			fos.flush();
			fos.close();
			bis.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return file;
	}
	
	
	
	
	
	
	public static String generateURL(Long projectId){
		return Global.DNS_TEMP + Global.MOBILE_SITE_PATH+"/mom/pro/"+projectId+".html";
	}
	
	public static String generateProjectURL(Long projectId){
		String bgMark = "?bg=1";
//		return "http://192.168.1.102:8080/mobile" + Global.MOBILE_SITE_PATH +"/" + projectId + bgMark;
		return Global.DNS +"/zt/f/site/"+projectId;
	}
	
	/**
	 * @author Jakemanse
	 * @time 2013-7-1  下午2:47:12
	 * @function <p>  Base Path  即  http://localhost:80/zxims</p>
	 * @param basePath
	 * @param ecardId
	 * @return
	 */
	public static String generateEcardURL(String basePath, Long ecardId){
		return basePath  + Global.MOBILE_ECARD_PATH +"/"+ecardId+Global.URL_SUFFIX;
	}
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-7-28  上午2:06:29
	 * @function <p> 生成礼物的URL如：http://weixin.zhaotang.net/zt/f/gift/5.html </p>
	 * @param giftId
	 * @return
	 */
	public static String generateGiftURL(Long giftId){
		return Global.DNS + Global.CONTEXT_PATH + Global.MOBILE_GIFT_PATH +"/"+giftId+Global.URL_SUFFIX;
	}
	
	/**
	 * @author Jakemanse
	 * @time 2013-7-31  下午3:51:44
	 * @function <p> 生成旅游车队管理的URL 如：http://weixin.zhaotang.net/zt/f/travel/bus/5.html </p>
	 * @param giftId
	 * @return
	 */
	public static String generateTravelBusURL(Long busId){
		String url = Global.DNS + Global.CONTEXT_PATH + Global.MOBILE_TRAVEL_BUS_PATH +"/view/"+busId+Global.URL_SUFFIX;
		String param = "?type=qrcodex$xp1="+System.currentTimeMillis()+getRandomString(50);
		return url + param;
	}
	
	
	public static String getRandomString(int len){
		StringBuffer sb = new StringBuffer();
		String [] chars = {"a","b","c","D","E","f","g","x","z","Y","U","I","D","H","j","Z","P","o","O"};
		Random rand = new Random();
		for(int i=0;i<len;i++){
			sb.append(rand.nextInt(10));
			//sb.append(chars[rand.nextInt(15)]);
		}
		return sb.toString();
	}
	
	
	
	
	
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-7-22  上午11:37:26
	 * @function <p> 优惠券 序列号 </p>
	 * @param userId
	 * @return
	 */
	public static String  generateCouponSN(Long userId){
		StringBuffer sb = new StringBuffer();
		sb.append(QRCODE_VTYPE_COUPON).append(userId).append("-").append(generateUUID());
		return sb.toString();
	}
	
	
	/**
	 * @author Jakemanse
	 * @time 2014-7-10  下午3:58:54
	 * @function <p> 会员支付生成的二维码 </p>
	 * @param userId
	 * @return
	 */
	public static String  generatePayMoneySN(Long userId){
		StringBuffer sb = new StringBuffer();
		sb.append(QRCODE_VTYPE_PAY_MONEY).append(userId).append("-").append(generateUUID());
		return sb.toString();
	}
	
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-10-3  上午2:37:21
	 * @function <p> 积分兑换 </p>
	 * @param userId
	 * @return
	 */
	public static String  generateScoreExchageSN(Long userId){
		StringBuffer sb = new StringBuffer();
		sb.append(QRCODE_VTYPE_EXCHANGE).append(userId).append("-").append(generateUUID());
		return sb.toString();
	}
	
	/**
	 * @author Jakemanse
	 * @time 2014-1-28  下午11:29:25
	 * @function <p> 入场券 </p>
	 * @param userId
	 * @return
	 */
	public static String  generateTicketSN(Long userId){
		StringBuffer sb = new StringBuffer();
		sb.append(QRCODE_VTYPE_TICKET).append(userId).append("-").append(generateUUID());
		return sb.toString();
	}
	
	/**
	 * @author Jakemanse
	 * @time 2013-9-28  下午3:08:51
	 * @function <p> 秒杀获得者 的二维码电子凭证  + 序列号  </p>
	 * @param userId
	 * @return
	 */
	public static String generateSeckillSN(Long userId){
		StringBuffer sb = new StringBuffer();
		sb.append(QRCODE_VTYPE_SECKILL).append(userId).append("-").append(generateUUID());
		return sb.toString();
	}
	
	/**
	 * @author Jakemanse
	 * @time 2013-9-30  下午3:29:40
	 * @function <p> 抽奖序列号 </p>
	 * @param userId
	 * @return
	 */
	
	public static String generateLotterySN(Long userId){
		StringBuffer sb = new StringBuffer();
		sb.append(QRCODE_VTYPE_LOTTERY).append(userId).append("-").append(generateUUID());
		return sb.toString();
	}
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-10-2  下午11:00:06
	 * @function <p>微信朋友圈图片分享 </p>
	 * @param userId
	 * @return
	 */
	public static String generateCircleShareSN(Long userId){
		StringBuffer sb = new StringBuffer();
		sb.append(QRCODE_VTYPE_CIRCLE_SHARE).append(userId).append("-").append(generateUUID());
		return sb.toString();
	}
	
	
	
	/**
	 * @author Jakemanse
	 * @time 2014-1-3  下午11:01:07
	 * @function <p> 赞活动二维码  </p>
	 * @param userId
	 * @return
	 */
	public static String generatePraiseActivitySN(Long userId){
		StringBuffer sb = new StringBuffer();
		sb.append(QRCODE_VTYPE_PRAISE_ACTIVITY).append(userId).append("-").append(generateUUID());
		return sb.toString();
	}
	
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-10-2  下午11:11:48
	 * @function <p> 生成唯一ID或序列号 </p>
	 * @return
	 */
	public  static String generateUUID(){
		String str = UUID.randomUUID().toString().toUpperCase();
		return str.replaceAll("-", "");
	}
	
	
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-7-23  下午11:50:16
	 * @function <p> 二维码的下载路径 </p>
	 * @param qrCodeId
	 * @return
	 */
	public static String getQRCodeURL(Long qrCodeId){
		//return QRCODE_JPG_URL_PRIX+qrCodeId+"&rnd="+System.currentTimeMillis();
		return QRCODE_JPG_URL_PRIX+qrCodeId;
	}
	
	/**
	 * @author Jakemanse
	 * @time 2013-10-3  上午12:19:21
	 * @function <p> 二维码图片的查看和下载路径  </p>
	 * @param qrCodeId
	 * @return
	 */
	public static String getQRCodePNGURL(Long qrCodeId){
		//return QRCODE_PNG_URL_PRIX+qrCodeId+"&rnd="+System.currentTimeMillis();
		return QRCODE_PNG_URL_PRIX+qrCodeId;
	}
	
	/**
	 * @author Jakemanse
	 * @time 2013-11-26  下午11:16:34
	 * @function <p> 二维码电子凭证的浏览页面 </p>
	 * @param qrCodeId
	 * @return
	 */
	public static String getQRCodePNGPageURL(Long qrCodeId){
		return QRCODE_PNG_PAGE_URL_PRIX+qrCodeId+"&rnd="+System.currentTimeMillis();
	}

}
